﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLVBDQ.UIControl;
using DTO;
using BUS;

namespace QLVBDQ
{
    /// <summary>
    /// Interaction logic for ThongTinNo.xaml
    /// </summary>
    public partial class ThongTinNo : UserControl
    {
        public ThongTinNo()
        {
            InitializeComponent();
        }

        private void listKhoanNo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadChiTietPhieuNo();
        }

        private void UserControl_Initialized_1(object sender, EventArgs e)
        {
            KhoiTaoMaKhachHangAuto();
            maKhachHang.MakeFocus();
        }

        /// <summary>
        /// Lấy mã khách hàng quen thui
        /// </summary>
        private void KhoiTaoMaKhachHangAuto()
        {
            List<string> list = KhachHang_BUS.Instance.LayToanBoMaKhachHangQuen();
            for (int i = 0; i < list.Count; i++)
                maKhachHang.AddItem(new AutoCompleteEntry(list[i], list[i]));
        }

        private void maKhachHang_LostFocus(object sender, RoutedEventArgs e)
        {
            if (maKhachHang.IsElementFocused())
                return;

            TuDongDienThongTinKhachHang();
            TuDongDienThongTinNo();
        }

        /// <summary>
        /// Tự động điền thông tin khách hàng khi biết mã khách hàng
        /// </summary>
        void TuDongDienThongTinKhachHang()
        {
            KhachHang_DTO kh = KhachHang_BUS.Instance.LayThongTinKhachHang(maKhachHang.Text);
            if (kh == null)
            {
                tenKH.Text = string.Empty;
                tienNo.Text = string.Empty;
                return;
            }
            tenKH.Text = kh.TenKhachHang;
        }

        /// <summary>
        /// Tự động điền thông tin nợ
        /// </summary>
        void TuDongDienThongTinNo()
        {
            listNo.Items.Clear();
            // Load danh sach phiếu bán hàng
            long conlai = 0;
            List<PhieuBanHang_DTO> list = PhieuBanHang_BUS.Instance.LayDanhSachPhieuBanHangTraCham(maKhachHang.Text);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].STT = i + 1;
                listNo.Items.Add(list[i]);
                PhieuNo_DTO phieuNoCuoi = PhieuNo_BUS.Instance.LayPhieuNoCuoi(list[i].MaPhieuBanHang);
                conlai += phieuNoCuoi.ConLai;
            }
            tienNo.Text = conlai.ToString();
        }

        void LoadChiTietPhieuNo()
        {
            if (listNo.SelectedIndex == -1)
                return;
            listPhieuNo.Items.Clear();
            List<PhieuNo_DTO> list = PhieuNo_BUS.Instance.LayDanhSachPhieuNo(((PhieuBanHang_DTO)listNo.SelectedItem).MaPhieuBanHang);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].STT = i + 1;
                listPhieuNo.Items.Add(list[i]);
            }
        }

        private void butXemThongTin_Click(object sender, RoutedEventArgs e)
        {
            TuDongDienThongTinKhachHang();
            TuDongDienThongTinNo();
        }
    }
}
